<template>
  <div>
    <!--搜索表单-->
    <div style="margin-bottom: 10px"></div>
    <div>
      <el-input style="width: 150px;" placeholder="请输入分类名称" v-model="params.name"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search">搜索</i></el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right">重置</i></el-button>
    </div>
    <div style="margin-bottom: 10px"></div>
    <el-table :data="tableData" border stripe row-key="id" default-expand-all>
      <el-table-column prop="id" label="编号" width="80px"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>

      <el-table-column label="操作" width="250px">
        <template v-slot="scope">
          <el-button type="success" v-if="!scope.row.pid" @click="openAddDialog(scope.row)">添加</el-button>
          <el-button type="primary" @click="$router.push('/editCategory?id='+scope.row.id)">编辑</el-button>
          &nbsp;&nbsp;
          <el-popconfirm
              title="确认删除这行数据吗？"
              @confirm="del(scope.row.id)">
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div style="margin-top: 20px"></div>
    <!--  分页  -->
    <div>
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <div>
      <el-dialog title="添加二级分类" :visible.sync="dialogFormVisible" width="30%" style="text-align: center">
        <el-form :model="form" :rules="addBookRules" ref="bookRef">
          <el-form-item prop="name" label="分类名称"  :label-width="formLabelWidth" style="width: 85%">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="remark" label="分类备注"  :label-width="formLabelWidth" style="width: 85%">
            <el-input v-model="form.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBook()">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";
export default {
  name: 'CategoryList',
  data(){
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      tableData: [],
      total: 0,
      pid:null,
      dialogFormVisible:false,
      form:{},
      formLabelWidth: '100px',
      params: {
        pageNum: 1,
        pageSize: 10,
        name: '',
      },
      addBookRules:{
        name:[{required:true,message:"请输入名称",trigger:"blur"}],
        remark:[{required:true,message:"请输入备注",trigger:"blur"}]
      }
    }
  },
      created() {
        console.log("==================")
        this.load()
      },
      methods: {
        openAddDialog(row){
          this.pid = row.id
          this.dialogFormVisible=true
        },
        addBook(){
            this.$refs["bookRef"].validate((valid) =>{
              if (valid){
                this.form.pid = this.pid
                request.post("/category/save",this.form).then(res =>{
                  if (res.code === "200"){
                    this.$message.success("二级分类添加成功！")
                    this.$refs['bookRef'].resetFields()
                    this.dialogFormVisible=false
                    this.load()
                  }else{
                    this.$message.error("二级分类添加成功！")
                  }
                })
              }
            })
        },
        load() {
          request.get('/category/page', {params: this.params}).then(res => {
            if (res.code === '200') {
              this.tableData = res.data.list
              this.total = res.data.total
            }
          })
        },
        reset() {
          this.params = {
            pageNum: 1,
            pageSize: 10,
            username: '',
            phone: '',
            email: ''
          }
          this.load()
        },
        handleCurrentChange(pageNum) {
          //点击分页按钮出发分页
          this.params.pageNum = pageNum
          this.load()
        },
        del(id) {
          request.delete("/category/delete/" + id).then(res => {
            if (res.code === '200') {
              this.$notify.success("删除OK!")
              this.load()
            } else {
              this.$notify.error(res.code)
            }
          })
        }
      }}


</script>

<style scoped>

</style>
